const CryptoJS = require('crypto-js')
const moment = require('./moment')

export function formatTime(seconds: any) {
    var hours = Math.floor(seconds / 3600)
    var minutes = Math.floor((seconds % 3600) / 60)
    var remainingSeconds = seconds % 60

    // 使用字符串模板构建格式化后的时间字符串
    var formattedTime = `${hours}小时 ${minutes}分钟 ${remainingSeconds}秒`

    return formattedTime
}

const formatNumber = (n: number) => {
    const s = n.toString()
    return s[1] ? s : '0' + s
}

export const getMd5Sign = (body: any) => {
    const date = moment.formatTime(null, 'YYYY-MM-DD')
    const message = `Cat_${JSON.stringify(body)}${date.replaceAll('-', '')}`
    return CryptoJS.MD5(message).toString()
}

/**
 * 节流
 * @param func
 * @param wait
 */
export function throttle(func: Function, wait = 300) {
    let timeout: any
    return function (this: any) {
        const context = this
        const args = arguments
        if (!timeout) {
            timeout = setTimeout(function () {
                timeout = null
                func.apply(context, args)
            }, wait)
        }
    }
}
